/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.adapter;

import java.util.List;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.activity.intent.StopMapIntentHolder;
import lt.marsrutai.mobile.android.model.ArrivalTime;
import lt.marsrutai.mobile.android.model.enums.TransportType;
import lt.marsrutai.mobile.android.util.HourMinuteFormat;
import lt.marsrutai.mobile.android.widget.ListViewAdapter;
import lt.marsrutai.mobile.android.widget.ExpectedArrivalTimeTextView;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

public class NextArrivalTimeAdapter extends ListViewAdapter<ArrivalTime> {

    private String scopeFolder;
    
    public NextArrivalTimeAdapter(Context context, String scopeFolder) {
        super(context, R.layout.nearest_routes_item);
        
        this.scopeFolder = scopeFolder;
    }

    @Override
    public View doGetView(int position, View view, ViewGroup parent) {

        final ArrivalTime arrivalTime = getItem(position);

        if (arrivalTime != null) {
            ImageButton mapButton = findViewById(view, R.id.map);
            mapButton.setFocusable(false);
            mapButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    StopMapIntentHolder intentHolder = new StopMapIntentHolder(activity);
                    intentHolder.setScopeFolder(scopeFolder);
                    intentHolder.setTrackId(arrivalTime.getTrackId());
                    intentHolder.setStopId(arrivalTime.getStopId());
                    intentHolder.setScheduleName(arrivalTime.getScheduleName());
                    intentHolder.setTransportId(arrivalTime.getTransportId());
                    intentHolder.setTitle(arrivalTime.getStopName() + ", " + arrivalTime.getStopDirectionName());
                    activity.startActivity(intentHolder.getIntent());
                }
            });
            
            TextView scheduleName = (TextView) view.findViewById(R.id.schedule_name);
            TextView routeName = (TextView) view.findViewById(R.id.route_name);
            TextView scheduledTime = (TextView) view.findViewById(R.id.scheduled_time);
            ExpectedArrivalTimeTextView expectedTime = (ExpectedArrivalTimeTextView) view.findViewById(R.id.expected_time);

            if (scheduleName != null && !TransportType.TRAIN.equals(arrivalTime.getTransportType())) {
                scheduleName.setText(arrivalTime.getScheduleName());
                scheduleName.setBackgroundColor(AppContext.getTransportInfoResolver().resolveTransportColor(scopeFolder, arrivalTime.getTransportId()));
            }
            if (routeName != null) {
                routeName.setText(arrivalTime.getDirectionName());
            }
            if (scheduledTime != null) {
                scheduledTime.setText(HourMinuteFormat.format(arrivalTime.getScheduledTime()));
            }
            if (expectedTime != null) {
                expectedTime.setArrivalTime(arrivalTime);
            }
      
        }
        return view;
    }

    public void setArrivalTimes(List<ArrivalTime> arrivalTimes) {
        if (null == arrivalTimes) {
            return;
        }

        for (int i = 0; i < arrivalTimes.size(); i++) {
            add(arrivalTimes.get(i));
        }
    }

}
